#coding: utf-8

from cocos2d                            import *
from game.lib.SceneTransition           import SceneTransition
from game.lib.NetworkHelper             import NetClient
from game.lib.PositionHelper            import PositionHelper
from fight                              import FightScene
from game.common                        import *

import random

class MainScene(CCScene):
    def __init__(self):
        super(MainScene,self).__init__()

        p = 'bg/general.png'
        if random.choice(range(100)) % 2 == 0:
            p = 'bg/fight/0.png'
        bg = CCSprite(p)
        bg.Position = PositionHelper.getInstance().ScreenC
        self.addChild(bg)

        dlg = CCSprite(spriteFrameName='ui/main_bg.png')
        dlg.Position = PositionHelper.getInstance().ScreenC
        self.addChild(dlg)

        item1 = CCMenuItemImage('ui/start.png', 'ui/start_h.png'    , '', self.on_start     , True)
        item2 = CCMenuItemImage('ui/setting.png', 'ui/setting_h.png', '', self.on_setting   , True)
        item3 = CCMenuItemImage('ui/chm.png', 'ui/chm_h.png'        , '', self.on_chm       , True)
        item4 = CCMenuItemImage('ui/help.png', 'ui/help_h.png'      , '', self.on_help      , True)

        menu = CCMenu((item1, item2, item3, item4))
        menu.Position = PositionHelper.getInstance().ScreenC
        menu.alignItemsVerticallyWithPadding(10)
        self.addChild(menu)

        n = NetClient.create_net_client('112.124.34.240', 8002, 0x100, self)
        n.send({0x0B: 1, 0x10: 0xFFF0})


    def on_start(self, *l, **kw):
        print 'on_start'
        fini_scene = FightScene()
        SceneTransition.getInstance().randomTransition(0.3, fini_scene)

    def on_setting(self,  *l, **kw):
        print 'on_setting'

    def on_chm(self,  *l, **kw):
        particle = CCParticleSpiral()
        particle.AutoRemoveOnFinish = True
        particle.PositionType = kCCPositionTypeFree
        particle.Position = PositionHelper.getInstance().ScreenC
        self.addChild(particle)
        print 'on_chm'

    def on_help(self,  *l, **kw):
        fini_scene = MainScene()
        SceneTransition.getInstance().randomTransition(0.3, fini_scene)
        print 'on_help'